package cn.firecode.data_collect.controller;


import cn.firecode.data_collect.domain.entity.CollectObjectT;
import cn.firecode.data_collect.domain.entity.ObjectVi;
import cn.firecode.data_collect.service.CollectObjectTService;
import cn.firecode.data_collect.service.ObjectViService;
import cn.firecode.data_collect.utils.DataResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangfan
 * @since 2019-12-10
 */
@RestController
@RequestMapping("/object")
@Slf4j
public class CollectObjectTController {

    @Autowired
    private CollectObjectTService objectTService;

    @Autowired
    private ObjectViService objectViService;

    @RequestMapping(value = "/getList" ,method = RequestMethod.GET)
    public DataResponse getList(){
//        QueryWrapper<CollectObjectT> wrapper = new QueryWrapper<>();
//        wrapper.eq("is_collect",0);
//        List<Object> objects = objectTService.listObjs(wrapper);
        List<ObjectVi> objects = objectViService.list();
        for(int i =0 ;i<objects.size();i++){
            objects.get(i).setThumA("http://192.168.1.218:8410/"+objects.get(i).getThumA());
            objects.get(i).setThumS("http://192.168.1.218:8410/"+objects.get(i).getThumS());
            objects.get(i).setThumC("http://192.168.1.218:8410/"+objects.get(i).getThumC());
        }
        return DataResponse.succeed(String.valueOf(objects.size()),objects);
    }

    @RequestMapping(value = "/add" ,method = RequestMethod.POST)
    public DataResponse add(String describe, String species, String type, Integer preoperativeCt, Integer boardId){
        log.info("新增采集对象");
        CollectObjectT objectT = new CollectObjectT();
        objectT.setBoardId(boardId);
        objectT.setPreoperativeCt(preoperativeCt);
        objectT.setObjectType(type);
        objectT.setObjectSpecies(species);
        objectT.setObjectDescribe(describe);
        objectT.setCreateTime(LocalDateTime.now());
        objectT.setIsCollect(0);
        boolean save = objectTService.save(objectT);
        if(save){
            log.info("新增采集对象成功:"+objectT.toString());
            return DataResponse.succeed("新增成功");
        }else{
            return DataResponse.failed(1,"新增失败");
        }

    }


    @RequestMapping(value = "/delete/{objectId}", method = RequestMethod.GET)
    public DataResponse delete(@PathVariable("objectId")Integer objectId){

        CollectObjectT objectT = objectTService.getById(objectId);
        if(objectT==null){
            return DataResponse.failed(1, "删除失败,无有效记录");
        }
        objectT.setIsCollect(2);
        boolean result = objectTService.saveOrUpdate(objectT);
        if(result){
            log.info("删除采集对象:"+objectT.toString());
            return DataResponse.succeed("删除成功");
        }else{
            return DataResponse.failed(1,"删除失败");
        }

    }

}
